<template>
    <div>
        <h3>添加菜谱</h3>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">菜谱标题<span style="color: red;">*</span></td>
                    <td>
                        <input type="text" v-model="info.caseName">
                    </td>
                </tr>
                <tr>
                    <td align="right">菜谱图片<span style="color: red;">*</span></td>
                    <td>
                        <input type="file" @change="Imgs">
                        <img :src="info.caseImg" v-if="info.caseImg != ''" width="50" height="50">
                    </td>
                </tr>
                <tr>
                    <td align="right">标签<span style="color: red;">*</span></td>
                    <td>
                        <select v-model="info.caseBard">
                            <option value="">请选择</option>
                            <option v-for="item in by" :value="item">{{ item }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right">分类<span style="color: red;">*</span></td>
                    <td>
                        <select v-model="info.caseTypeId">
                            <option value="">请选择</option>
                            <option v-for="item in Bang" :value="item.caseTypeId">{{ item.caseTypeName }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right">发布时间<span style="color: red;">*</span></td>
                    <td>
                        <input type="date" v-model="info.caseTime">
                    </td>
                </tr>
                <tr>
                    <td align="right"></td>
                    <td>
                        <input type="button" value="添加" class="btn btn-success" @click="AddCase">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router';
import moment from 'moment';
const router = useRouter()
const route = useRoute()
onMounted(() => {
    BangType();
})
//标签下拉框
let by: any = ref(['低盐', '低糖', '高糖'])
//绑定下拉框
let Bang: any = ref([])
const BangType = () => {
    axios({
        url: '/api/Emp/BangCaseType',
        method: 'get',
    })
        .then((res) => {
            console.log(res);
            Bang.value = res.data
        })
        .catch((err) => {
            console.log(err);

        })
}
//添加参数
let info: any = reactive({
    "caseName": "",
    "caseImg": "",
    "caseTypeId": '',
    "caseBard": "",
    "caseState": 1,
    "caseTime": "",
})
const AddCase = () => {
    if (info.caseName == "") {
        alert('菜谱标题不能为空')
        return
    }
    if (info.caseImg == "") {
        alert('菜谱图片不能为空')
        return
    }
    if (info.caseTypeId == "") {
        alert('菜谱类型必选')
        return
    }
    if (info.caseBard == "") {
        alert('标签必选')
        return
    }
    if (info.caseTime == "") {
        alert('时间不能为空')
        return
    }
    axios({
        url: '/api/Emp/AddCase',
        method: 'post',
        data: info
    })
        .then((res) => {
            console.log(res);
            if (res.data == -1) {
                alert('菜谱标题重复')
                return
            }
            if (res.data > 0) {
                alert('添加成功')
                router.push('/showcase')
            }
            else {
                alert('添加失败')
            }
        })
        .catch((err) => {
            console.log(err);

        })
}
const Imgs = (e: any) => {
    let obj = e.target.files[0]
    let f = new FormData()
    f.append('file', obj)
    axios({
        url: '/api/Emp/Imgs',
        method: 'post',
        data: f
    })
        .then((res) => {
            console.log(res);
            if (res.data == '不是图片') {
                alert('不是图片')
                return
            }
            if (res.data == '大于2M') {
                alert('大于2M')
                return
            }
            info.caseImg = res.data
        })
        .catch((err) => {
            console.log(err);

        })
}
</script>

<style scoped></style>